home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / animation / smooth_arm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.1 KB  |  182 lines

  1. /*
  2.  * Copyright 1995, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*  smooth_arm.c - animates a robot arm in double buffered mode to 
  19.  *            create smooth motion
  20.  *
  21.  *  <r> Key            - toggle arm rotation on / off
  22.  *  Escape key              - exit the program 
  23.  */
  24.  
  25. #include <GL/gl.h>
  26. #include <GL/glu.h>
  27. #include <GL/glut.h>
  28. #include <math.h>    /* for fmodf */
  29. #include <stdlib.h>    /* for NULL */
  30. #include <stdio.h>    /* for printf */
  31.  
  32. /*  Function Prototypes  */
  33.  
  34. GLvoid  initgfx( GLvoid );
  35. GLvoid  animate( GLvoid );
  36. GLvoid  visibility( GLint );
  37. GLvoid  drawScene( GLvoid );
  38. GLvoid  reshape( GLsizei, GLsizei );
  39. GLvoid  keyboard( GLubyte, GLint, GLint );
  40.  
  41. void printHelp( char * );
  42.  
  43. /*  Global Variables */
  44.  
  45. static GLfloat    shoulderAngle = 0.0;    /* controls shoulder rotation */
  46.  
  47. static GLboolean    rotateFlag = GL_TRUE;
  48.  
  49. /* Global Definitions */
  50.  
  51. #define KEY_ESC    27    /* ascii value for the escape key */
  52.  
  53. void
  54. main( int argc, char *argv[] )
  55. {
  56.     GLsizei width, height;
  57.  
  58.     glutInit( &argc, argv );
  59.  
  60.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  61.     height = glutGet( GLUT_SCREEN_HEIGHT );
  62.     glutInitWindowPosition( 0, height / 4 );
  63.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  64.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  65.     glutCreateWindow( argv[0] );
  66.  
  67.     initgfx();
  68.  
  69.     glutIdleFunc( animate );
  70.     glutVisibilityFunc( visibility );
  71.     glutKeyboardFunc( keyboard );
  72.     glutReshapeFunc( reshape );
  73.     glutDisplayFunc( drawScene ); 
  74.  
  75.     printHelp( argv[0] );
  76.  
  77.     glutMainLoop();
  78. }
  79.  
  80. void
  81. printHelp( char *progname )
  82. {
  83.     fprintf(stdout, "\n%s - smoothly animate a robot arm\n\n"
  84.         "Axes: X - red, Y - green, Z - blue\n\n"
  85.         "<r> Key        - toggle arm rotation on / off\n"
  86.         "Escape Key        - exit the program\n",
  87.         progname);
  88. }
  89.  
  90. GLvoid
  91. initgfx( GLvoid )
  92. {
  93.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  94.     glShadeModel( GL_FLAT );
  95.     glEnable( GL_DEPTH_TEST );
  96. }
  97.  
  98. GLvoid
  99. reshape( GLsizei width, GLsizei height )
  100. {
  101.     GLdouble    aspect;
  102.  
  103.     glViewport( 0, 0, width, height );
  104.  
  105.     aspect = (GLdouble) width / (GLdouble) height;
  106.  
  107.     glMatrixMode( GL_PROJECTION );
  108.     glLoadIdentity();
  109.     gluPerspective( 45.0, aspect, 1.0, 20.0 );
  110.     glMatrixMode( GL_MODELVIEW );
  111. }
  112.  
  113. GLvoid 
  114. keyboard( GLubyte key, GLint x, GLint y )
  115. {
  116.     switch (key) {
  117.     case 'r':
  118.         rotateFlag = !rotateFlag;
  119.         if (rotateFlag) {
  120.             glutIdleFunc( animate );
  121.         } else {
  122.             glutIdleFunc( NULL );
  123.         }
  124.         break;
  125.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  126.         exit(0);
  127.     }
  128. }
  129.  
  130. GLvoid 
  131. animate( GLvoid )
  132. {
  133.     /* update the rotation of the shoulder for each scene */
  134.     shoulderAngle = fmodf( (shoulderAngle + 1.0), 360.0 );
  135.  
  136.     glutPostRedisplay();    /* Tell GLUT to redraw the scene */
  137. }
  138.  
  139. GLvoid
  140. visibility( int state ) 
  141. {
  142.     /* restart the animation function if we were
  143.      * animated when the window was hidden
  144.      */
  145.     if (state == GLUT_VISIBLE && rotateFlag) {
  146.         glutIdleFunc( animate );
  147.     } else {
  148.         glutIdleFunc( NULL );
  149.     }
  150. }
  151.  
  152. GLvoid
  153. drawScene( GLvoid )
  154. {
  155.     static GLfloat    upperArmColor[] = { 1.0, 0.0, 0.0 };
  156.     static GLfloat    lowerArmColor[] = { 0.8, 0.5, 0.5 };
  157.  
  158.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  159.  
  160.     glPushMatrix();
  161.         gluLookAt( 0.0, 0.0, 8.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
  162.         XYaxes();
  163.  
  164.         /* rotate the shoulder by shoulderAngle degrees */
  165.         glRotatef( shoulderAngle, 0.0, 0.0, 1.0 );
  166.  
  167.         /* Draw the upper arm */
  168.         glTranslatef( 1.0, 0.0, 0.0 ); 
  169.             glColor3fv( upperArmColor );
  170.         WireBox( 2.0, 0.4, 1.0 );
  171.  
  172.         /* Draw the lower arm */
  173.         glTranslatef( 1.0, 0.0, 0.0 );
  174.         glRotatef( 45.0, 0.0, 0.0, 1.0 );
  175.         glTranslatef( 1.0, 0.0, 0.0 );
  176.             glColor3fv( lowerArmColor );
  177.         WireBox( 2.0, 0.4, 1.0 );
  178.     glPopMatrix();
  179.  
  180.     glutSwapBuffers();
  181. }
  182.